{
 "cells": [
  {
   "cell_type": "raw",
   "id": "a016701c",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_label: Anthropic\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf733a38-db84-4363-89e2-de6735c37230",
   "metadata": {},
   "source": [
    "# ChatAnthropic\n",
    "\n",
    "This notebook covers how to get started with Anthropic chat models.\n",
    "\n",
    "## Setup\n",
    "\n",
    "For setup instructions, please see the Installation and Environment Setup sections of the [Anthropic Platform page](/docs/integrations/platforms/anthropic.mdx)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91be2e12",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -qU langchain-anthropic"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "584ed5ec",
   "metadata": {},
   "source": [
    "## Environment Setup\n",
    "\n",
    "We'll need to get a [Anthropic](https://console.anthropic.com/settings/keys) and set the `ANTHROPIC_API_KEY` environment variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01578ae3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "\n",
    "os.environ[\"ANTHROPIC_API_KEY\"] = getpass()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1f9df276476f0bc",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "The code provided assumes that your ANTHROPIC_API_KEY is set in your environment variables. If you would like to manually specify your API key and also choose a different model, you can use the following code:\n",
    "```python\n",
    "chat = ChatAnthropic(temperature=0, anthropic_api_key=\"YOUR_API_KEY\", model_name=\"claude-instant-1.2\")\n",
    "\n",
    "```\n",
    "Please note that the default model is \"claude-2,\" and you can check the available models at [here](https://docs.anthropic.com/claude/reference/selecting-a-model)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-19T11:25:07.274418Z",
     "start_time": "2024-01-19T11:25:05.898031Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content=' 저는 파이썬을 좋아합니다.')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_anthropic import ChatAnthropic\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "chat = ChatAnthropic(temperature=0, model_name=\"claude-2\")\n",
    "\n",
    "system = (\n",
    "    \"You are a helpful assistant that translates {input_language} to {output_language}.\"\n",
    ")\n",
    "human = \"{text}\"\n",
    "prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n",
    "\n",
    "chain = prompt | chat\n",
    "chain.invoke(\n",
    "    {\n",
    "        \"input_language\": \"English\",\n",
    "        \"output_language\": \"Korean\",\n",
    "        \"text\": \"I love Python\",\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c361ab1e-8c0c-4206-9e3c-9d1424a12b9c",
   "metadata": {},
   "source": [
    "## `ChatAnthropic` also supports async and streaming functionality:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c5fac0e9-05a4-4fc1-a3b3-e5bbb24b971b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-19T11:25:10.448733Z",
     "start_time": "2024-01-19T11:25:08.866277Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content=\" Why don't bears like fast food? Because they can't catch it!\")"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat = ChatAnthropic(temperature=0, model_name=\"claude-2\")\n",
    "prompt = ChatPromptTemplate.from_messages([(\"human\", \"Tell me a joke about {topic}\")])\n",
    "chain = prompt | chat\n",
    "await chain.ainvoke({\"topic\": \"bear\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "025be980-e50d-4a68-93dc-c9c7b500ce34",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-19T11:25:24.438696Z",
     "start_time": "2024-01-19T11:25:14.687480Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Here are some of the most famous tourist attractions in Japan:\n",
      "\n",
      "- Tokyo - Tokyo Tower, Tokyo Skytree, Imperial Palace, Sensoji Temple, Meiji Shrine, Shibuya Crossing\n",
      "\n",
      "- Kyoto - Kinkakuji (Golden Pavilion), Fushimi Inari Shrine, Kiyomizu-dera Temple, Arashiyama Bamboo Grove, Gion Geisha District\n",
      "\n",
      "- Osaka - Osaka Castle, Dotonbori, Universal Studios Japan, Osaka Aquarium Kaiyukan \n",
      "\n",
      "- Hiroshima - Hiroshima Peace Memorial Park and Museum, Itsukushima Shrine (Miyajima Island)\n",
      "\n",
      "- Mount Fuji - Iconic and famous mountain, popular for hiking and viewing from places like Hakone and Kawaguchiko Lake\n",
      "\n",
      "- Himeji - Himeji Castle, one of Japan's most impressive feudal castles\n",
      "\n",
      "- Nara - Todaiji Temple, Nara Park with its bowing deer, Horyuji Temple with some of world's oldest wooden structures  \n",
      "\n",
      "- Nikko - Elaborate shrines and temples nestled around Nikko National Park\n",
      "\n",
      "- Sapporo - Snow"
     ]
    }
   ],
   "source": [
    "chat = ChatAnthropic(temperature=0.3, model_name=\"claude-2\")\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [(\"human\", \"Give me a list of famous tourist attractions in Japan\")]\n",
    ")\n",
    "chain = prompt | chat\n",
    "for chunk in chain.stream({}):\n",
    "    print(chunk.content, end=\"\", flush=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
